local sz_canhua = fk.CreateSkill {
    name = "sz_canhua",
    tags = { Skill.Compulsory },
}

Fk:loadTranslationTable {
    ["sz_canhua"] = "残花",
    [":sz_canhua"] = "出牌阶段开始时，你可以令一名角色于你的下个准备阶段前视为拥有〖绝情〗；该角色体力变化时，你可以与其各失去一点体力，然后将其体力值调整至与你相同，然后本回合此技能失效。",

    ["$sz_canhua1"] = "大智若愚，女子之锦绣常隐于华服。",
    ["$sz_canhua2"] = "知者不惑，心有明镜以照人。",

    ["@@sz_canhua-turn"] = "残花失效",
    ["#sz_canhua-ask"] = "残花：你可以令你与 %src 共同失去一点体力，然后将 %src 体力调整至与你相同",
}

sz_canhua:addEffect(fk.EventPhaseStart, {
    anim_type = "special",
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(sz_canhua.name) and player == target and player.phase == Player.Play
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local targets = {}
        for _, id in ipairs(room.alive_players) do
            if not id:hasSkill("sz_jueqing") then
                table.insertIfNeed(targets, id)
            end
        end
        local to = room:askToChoosePlayers(player,
            {
                targets = targets,
                min_num = 1,
                max_num = 1,
                prompt = "残花：请选择一名获得〖绝情〗的角色",
                skill_name = sz_canhua.name,
                cancelable = true
            })
        if #to > 0 then
            event:setCostData(self, { tos = to })
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        room:handleAddLoseSkills(to, "sz_jueqing")
        room:addTableMarkIfNeed(player, "sz_canhua", to.id)
    end,
})

sz_canhua:addEffect(fk.EventPhaseStart, {
    anim_type = "special",
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(sz_canhua.name) and player == target and player.phase == Player.Start
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local tos = {}
        for _, id in ipairs(room:getOtherPlayers(player)) do
            if id:hasSkill("sz_jueqing") and table.contains(player:getTableMark("sz_canhua"), id.id) then
                table.insertIfNeed(tos, id)
            end
        end
        if #tos > 0 then
            room:handleAddLoseSkills(tos[1], "-sz_jueqing")
            room:setPlayerMark(player, "sz_canhua", 0)
        end
    end,
})

sz_canhua:addEffect(fk.HpChanged, {
    anim_type = "special",
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(sz_canhua.name) and target ~= player and target.hp ~= player.hp and
            table.contains(player:getTableMark("sz_canhua"), target.id) and data.num ~= 0 and
            player:getMark("@@sz_canhua-turn") == 0
    end,
    on_cost = function(self, event, target, player, data)
        return player.room:askToSkillInvoke(player, {
            skill_name = sz_canhua.name,
            prompt = "#sz_canhua-ask:" .. target.id
        })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player, "@@sz_canhua-turn", 1)
        local n = math.abs(player.hp - target.hp)
        room:loseHp(player, 1, sz_canhua.name)
        room:loseHp(target, 1, sz_canhua.name)
        if player.hp > target.hp then
            room:changeHp(target, n, nil, sz_canhua.name)
        else
            room:changeHp(target, -n, nil, sz_canhua.name)
        end
    end,
})

return sz_canhua
